package org.smile.excel.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.Test;

/**
 * 这种读取Excel的方式应该配配置ODBC数据源.
 * @author CDV-DX7
 *s
 */
public class ExcelUtil {
	
    private String driverName = "org.smile.excel.jdbc.ExcelDriver";
    private String url = "jdbc:excel:file//D:/mytest.xls"; // 不设置数据源

    public     ExcelUtil(){}

    public void setDriverName(String driverName){
        this.driverName = driverName;
    }

    @Test
    public void test(){
    	try {
			Connection conn=getJdbcConnection(driverName, url, null, null);
			Statement statement=conn.createStatement();
			ResultSet rs=statement.executeQuery("select id,name from 学生表 ");
			while(rs.next()){
				System.out.println(rs.getInt("id"));
				System.out.println(rs.getString("name"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
    }
    
    public static Connection getJdbcConnection(String driverName, String url, String userName, String password) throws SQLException {
		try {
			Class.forName(driverName);
			Connection conn = DriverManager.getConnection(url, userName, password);
			return conn;
		} catch (Exception e) {
			throw new SQLException("连接数据库[url：" + url + "]出现错误", e);
		}
	}
   
}